<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数组转对象</title>
    <script>
      //将相同数组项个数作为对象对应属性的值
      function arrConvertToObj(arr) {
        var obj = {};
        for (var item of arr) {
          //判断数组项对应属性是否定义
          if (obj[item] === undefined) {
            obj[item] = 1;
          } else {
            obj[item] += 1;
          }
        }
        console.log("数组：", arr, "\n转换后的对象：", obj);
      }
      arrConvertToObj(["a", "a", "a", "b", "b", "c", "c", "c", "c", "d", "d", "d", "d"]);
      //数组： (13) ["a", "a", "a", "b", "b", "c", "c", "c", "c", "d", "d", "d", "d"]
      //转换后的对象： {a: 3, b: 2, c: 4, d: 4}
    </script>
  </head>
  <body></body>
</html>
